package networkSimulation.simulation.virus.statistic

import networkSimulation.simulation.virus.InfectablePerson
import networkSimulation.framework.{Graph, Edge}
import networkSimulation.statistic.{Statistic, VisualStatistics}

/**
 * Statistic representing the amount of infected at the end as a bar chart.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
class EndAmountsVirusBar  [N<%InfectablePerson,E<%Edge[N]] extends Statistic[N,E]{
  private var infected = 0
  private var notInfected = 0
  private var dead = 0
  private var immune = 0
  private var infectedAmounts =List[Int]()
  private var notInfectedAmounts =List[Int]()
  private var deadAmounts =List[Int]()
  private var immuneAmounts =List[Int]()

  /*
  *  Generate the visual statistic
  */
  @Override
  override def generateStatistic(vis:VisualStatistics,path : String){
    var list = List[Int]()
    var strings = List[String]()
    strings::= "Infected"
    strings::= "Not Infected"
    strings::= "Dead"
    strings::= "Immune"
    list::=infectedAmounts.head
    list::=notInfectedAmounts.head
    list::=deadAmounts.head
    list::=immuneAmounts.head
        vis.createBarChart("Virus Spread End","Amount",list,strings,"Nodes",path)
  }

  /*
  *  Take snapshots from the graph
  */
  @Override
  override def updateStatistics(graph: Graph[N, E],k:String){
    infected = 0
    notInfected = 0
    dead = 0
    immune = 0
    for(node: N <- graph.nodes){
      if(node.infected ==true){
        infected+=1
      }else{
        if(node.isDead){
           dead+=1
        }else{
          notInfected+=1
          if(node.immune){
            immune+=1
          }
        }
      }
    }
    key = k
  }

  /*
  *  Load the xml data into the statistic
  */
  @Override
  override def loadData(data : xml.NodeSeq){
      infectedAmounts ::= (data \\ "Infected").text.toInt
      notInfectedAmounts ::= (data \\ "NotInfected").text.toInt
      deadAmounts ::= (data \\ "Dead").text.toInt
      immuneAmounts ::= (data \\ "Immune").text.toInt
  }

  /*
  *  Generates xml for the statistic
  */
  @Override
  override def toXml(): xml.NodeSeq  = {
    <Stat key={key}>
      <Infected>{ infected }</Infected>
      <NotInfected>{ notInfected }</NotInfected>
      <Dead>{ dead }</Dead>
      <Immune>{ immune }</Immune>
    </Stat>
  }
}